<?php

require_once( $CFG->dirroot . 'mod/folio/model/page.php');
require_once( $CFG->dirroot . 'mod/folio/model/user.php');
require_once( $CFG->dirroot . 'mod/folio/model/page_template.php');

require_once('base.php');

class folio_NewPage extends folio_BaseView {

    function __construct( folio_user $user, folio_page $homepage,
                         folio_page $parentpage, $page_title ) {
        $url = url;

        // Load Metatags
        $this->metatags = array(
                                'mod/folio/css/edit.css',
                                'mod/folio/js/new.js'
                                );

        // Start the form.
        $this->html .= <<< END
<img src="{$url}mod/folio/image/page_new.png"/><br>
<form method="post" name="page_new" action="{$url}mod/folio/ajax/page_insert.php">
<br/>
END;

        // Load subsidary controls
        $this->Load( new folio_NewPageTitle( $page_title ));
        //$this->Load( new folio_NewPageParentPage( $user, $parentpage ));

        $this->html .= "<br/>\n";

        $this->Load( new folio_NewPageFromTemplate( $user ));

        $this->html .= "\n";


        // NOte: unlike most ajax pages, insert doesn't need a redirection
        //    This is because we don't know the page name in advance, or know
        //    what its ID will be.  insert will automatically redirect to /edit
        //    on the new page.

        // Build the html controls
        $this->html .= <<< END
            <br/>
            <input type='hidden' name='page_format_ident' value='{$homepage->format_ident}' />
            <input type="hidden" name="page_user_ident" value="{$user->ident}" />
            <input type="submit" value="Create Page" />
            <INPUT TYPE="button" VALUE="Cancel" onClick="javascript:history.back()">
        </form>
END;

    }
}


/**
 * Setup the page title
 */
class folio_NewPageTitle extends folio_BaseView {

    /**
    * Default to 'title' or passed value.
    */
    function __construct( $title ) {

        $oTitle = __gettext('Make an empty new page');

        $url = url;
        $this->html .= <<< END
<div id='folio_NewPageTitle'>
    <b>{$oTitle}</b><br/>
    &nbsp;&nbsp;&nbsp;<input type="radio" name="folio_NewPageFromTemplate_page" value="_blank" checked=true>
    <input type="text" value="{$title}" name="page_title"/>
</div>
END;
    }
}

/**
 * Set the parent page of a new page
 */
class folio_NewPageParentPage extends folio_BaseView {

    function __construct( folio_user $user, folio_page $parentpage ) {
        $url = url;

        // Grab homepage for default value.  Homepage should never
        //  be created by this, as it should be made by a template.
        $homepage = folio_page::SelectHomepage($user);
        if( $homepage === false )
            trigger_error('You can not folio_PageEditorParentPage for a ' .
                            'new homepage', E_USER_ERROR);

        $parentpage_ident = $parentpage->page_ident;

        $oWhere = __gettext('Which page would you like this to be located under?');
        $html = folio_NewPageParentPage::recurse($homepage, $user,$homepage,$parentpage_ident);

        $this->html = <<< END
<h3>Parent Page</h3>
<div id='folio_NewPageParentPage'>
{$oWhere}<br/>
<SELECT NAME='page_parentpage_ident'>{$html}</SELECT>
</div>
END;
    }

    /**
     * Recursive function for building the parentcontrol.
     *
     * @param folio_page $editpage The page we're finding a new location for
     * @param folio_user $user Owner of the page
     * @param folio_page $page Page currently being recursed to
     * @param int $parentpage_ident Default value for the parentpage.
     * @param string $depth OPTIONAL, don't use for first call.
     */
    private function recurse( $editpage, $user, $page, $parentpage_ident, $depth = '') {

        // Build labels.
        if ($page->page_ident == $parentpage_ident ) {
            $oSelected = ' SELECTED=true';
        } else {
            $oSelected = '';
        }

        // Build label
        $run_result = "<OPTION VALUE={$page->page_ident} {$oSelected}>" .
                        $depth . $page->title . "\n";

        // Grab child pages.
        $pages = folio_page::SelectWhereParentpageIdent($page->page_ident);

        // Recurse for each child page.
        $depth .= "&nbsp;&nbsp;&nbsp;";
        if ( $pages !== false ) {
            // Iterate
            foreach ($pages as $p) {
                // Don't show the editpage or any of its children.
                if (
                    $editpage == false OR
                    ( $editpage !== false AND $p->page_ident != $editpage->page_ident )
                    ) {
                    $run_result .= folio_NewPageParentPage::recurse(
                                $editpage, $user, $p, $parentpage_ident, $depth );
                }
            }
        }

        // Return results.
        return $run_result;
    }
}

/**
 * Show a list of pages from the tempalte.
 */
class folio_SelectTemplate extends folio_BaseView {

    /**
    * The owning user of the portfolio.
    */
    function __construct( folio_user $user ) {

        $oTemplate = __gettext('Select a template to get your portfolio started');

        $templates = folio_page_template::SelectTemplates();
        $html = '';

        // Load from disk
        foreach( $templates as $template_title=>$template ) {
            $html .= "<option id='folio_SelectTemplate' value=\"{$template_title}\">{$template_title}</option>\n";
        }

        $this->html .= <<< END
<div id='folio_NewPageTemplate'>
<b>{$oTemplate}</b><br/><br/>
<select name="folio_SelectTemplate" size=8>
{$html}
</select>
</div>
END;
    }
}


/**
 * Show a list of pages from the tempalte.
 */
class folio_NewPageFromTemplate extends folio_BaseView {

    /**
    * The owning user of the portfolio.
    */
    function __construct( folio_user $user ) {

        $oTemplate = __gettext('Make a new page from a template');
        $options = folio_NewPageFromTemplate::_html($user);

        $this->html .= <<< END
<div id='folio_NewPageTemplate'>
<b>{$oTemplate}</b><br/>
{$options}
</div>
END;
    }


    /**
     * Return a html control to initialize a template.
     *
     * @param folio_user $user
     */
    private function _html( folio_user $user ) {

        $templates = folio_page_template::SelectTemplates();
        $html = '';

        // Load from disk
        foreach( $templates as $template_title=>$template ) {
            if( isset($template['optional_pages'])) {
                if( count($template['optional_pages'] > 0 )) {
                    $html .= "&nbsp;&nbsp;&nbsp;<i>{$template_title}</i><br/>\n";
                    foreach( $template['optional_pages'] as $title => $page) {
                        $html .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
                            "<input type='radio' name='folio_NewPageFromTemplate_page' " .
                            "value=\"{$template_title}::{$title}\">" .
                            "{$title}</option><br/>\n";
                    }
                }
            }
        }

        return $html;
    }
}

?>
